package com.example.mobile1920605114zyh0604;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
//创建服务，定义一个Servi的子类

public class MusicService extends Service {
    String TAG = "TAG";
    MediaPlayer mediaPlayer;

    class ServiceBinder extends Binder {
        public MusicService getMusicService() {
            return MusicService.this;
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
        
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: "+add(1,1));
        playMusic();
        return super.onStartCommand(intent, flags, startId);
     
        
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        return new ServiceBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }
    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        stopMusic();
        super.onDestroy();
    }
    public int add(int a , int b ){
        return a + b;
    }

    public void playMusic() {
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.old_boy);
        mediaPlayer.start();

    }

    public void pauseMusic() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    public void rePlauMusic() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }
    }

    public  void  stopMusic(){
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }

    }
}
